package gatperdut.chato.core.skills;

import gatperdut.chato.constants.CSConstantsSkills;
import gatperdut.chato.constants.CSConstantsString;
import gatperdut.chato.model.Chato.Constant.Skill;
import gatperdut.chato.model.Chato.Entity.Skill.KnownSkill;
import gatperdut.chato.model.Chato.Entity.Skill.SkillsContainer;

import java.util.List;

public class SkillsController {

    private final SkillsContainer skillsContainer;

    public SkillsController( SkillsContainer skillsContainer ) {
        this.skillsContainer = skillsContainer;
    }

    public boolean hasSkill( Skill skill ) {
        for ( KnownSkill s : getSkillsContainer().getKnownSkills() ) {
            if ( s.getSkill() == skill ) {
                return true;
            }
        }

        return false;
    }

    public int skillLevel( Skill skill ) {
        for ( KnownSkill s : getSkillsContainer().getKnownSkills() ) {
            if ( s.getSkill() == skill ) {
                return s.getLevel();
            }
        }

        throw new IllegalArgumentException( "Retrieving level for a non-learned skill." );
    }

    public List<KnownSkill> getKnownSkills() {
        return getSkillsContainer().getKnownSkills();
    }

    public SkillsContainer getSkillsContainer() {
        return skillsContainer;
    }

    public String listKnownSkills() {
        StringBuilder sb = new StringBuilder();

        int i = 0;
        for ( KnownSkill ks : getKnownSkills() ) {
            sb.append( "<" + CSConstantsSkills.getSkillName( ks.getSkill() ) + "> <" + ks.getLevel() + ">" );
            i++;
            if ( i < getKnownSkills().size() ) {
                sb.append( CSConstantsString.NEWLINE );
            }
        }

        return sb.toString();
    }

}
